package com.example.wan_android.viewmodel

import android.content.Intent
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.wan_android.http.bean.ProjectCategoryItemData
import com.example.wan_android.http.bean.ProjectContentsData
import com.example.wan_android.http.code.ProjectCategoryContentRespository
import com.example.wan_android.http.code.ProjectCategoryRepository
import com.example.wan_android.ui.activity.WebActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class ProjectViewModel : BaseViewModel() {
    private val repository = ProjectCategoryRepository()
    private val _projectCategoryLiveData = MutableLiveData<Result<List<ProjectCategoryItemData>>>()
    val projectCategoryLiveData = _projectCategoryLiveData
    private val _projectContentLiveData = MutableLiveData<Result<List<ProjectContentsData>>>()
    val projectContentLiveData = _projectContentLiveData
    val position = MutableLiveData<Int>(0)
    private val _isSuccess = MutableLiveData<Boolean>(false)
    val isSuccess = _isSuccess

    fun setPosition(p: Int) {
        position.value = p
    }

    init {
        getProjectCategory()
    }

    fun getProjectCategory() {
        viewModelScope.launch {
            _projectCategoryLiveData.value = repository.fetchProjectCategory()
        }
    }


    fun showProjectCategoryContent(id: Int) {
        _isSuccess.value = false
        viewModelScope.launch {
            // 使用局部变量避免污染成员变量
            var currentIndex = 0
            // 创建临时可变集合
            val accumulatedList = mutableListOf<ProjectContentsData>()

            try {
                do {
                    // 带线程调度的网络请求
                    val result = withContext(Dispatchers.IO) {
                        ProjectCategoryContentRespository().fetchProjectCategoryContent(
                            currentIndex,
                            id
                        )
                    }

                    // 成功时处理数据
                    if (result.isSuccess) {
                        result.getOrNull()?.let { data ->
                            if (data.isNotEmpty()) {
                                accumulatedList.addAll(data)
                                currentIndex++  // 索引自增准备下次请求
                            } else {
                                return@let  // 数据为空时退出循环
                            }
                        }
                    } else {
                        // 失败时传递错误
                        _projectContentLiveData.postValue(result)
                        return@launch
                    }
                } while (result.getOrNull()?.isNotEmpty() == true)

                // 最终传递合并结果
                _projectContentLiveData.postValue(Result.success(accumulatedList))

            } catch (e: Exception) {
                // 异常处理
                _projectContentLiveData.postValue(Result.failure(e))
            } finally {
                _isSuccess.value = true
            }
        }

    }

    fun openWeb(url: String) {
        startActivity(Intent().apply {
            putExtra(WebViewModel.url, url)
        }, targetClass = WebActivity::class.java)
    }
}